<?php

/**
 * Description of Form
 *
 * @author Rino7
 */
class Form {

    public $action;
    private $name;
    public $id;
    public $method;
    public $enctype = "multipart/form-data";

    /**
     * @var array AbstractComponent 
     */
    private $_components = array();
    private $_output = array("HTML" => "", "PHP" => "");
    private $_tableName = "";
    private $_oneToOneInserts = array();
    private $_oneToManyInserts = array();
    private $_oneToOneSaveLogicOutput = "";
    //@todo implementar estos 2 setters
    private $_outputlFilePath;
    private $_outputlFileName;

    /**
     * The head section into the html
     * @var string 
     */
    private $_headSection = '';

    const POST_METHOD = 'POST';
    const GET_METHOD = 'GET';

    public function __construct($name = "") {
        $this->method = self::POST_METHOD;
        $this->name = $name;
        $this->_outputlFilePath = $_SERVER["DOCUMENT_ROOT"] . '/public/';
        //@TODO sacar este hardcoding
        $this->_outputlFileName = 'UserForm.php';
    }

    private function _begin() {
        ob_start();
        require dirname(__FILE__) . '/RenderBusinessLogic/_beginForm.phtml';
        $beginSection = ob_get_clean();
        $this->_decorateHeadSection();
        $this->_output["HTML"] .= preg_replace("/{HEAD_SECTION}/", $this->_headSection, $beginSection);
    }

    private function _decorateHeadSection() {
        $this->_addJavascriptCalls();
    }

    private function _addJavascriptCalls() {
        $jsCalls = AbstractComponent::getJavascriptCalls();
        $numberOfJsCalls = count($jsCalls);
        $jsCallBuffer = "";
        for ($i = 0; $i < $numberOfJsCalls; ++$i) {
            $jsCallBuffer .= '<script type="text/javascript" src="' . $jsCalls[$i] . '" ></script>';
            $jsCallBuffer .= Utils::BREAK_LINE;
        }
        ob_start();
        require dirname(__FILE__) . '/RenderBusinessLogic/_headSection.phtml';
        $headSection = ob_get_clean();
        $this->_headSection = preg_replace("/{JAVASCRIPT_ADDED_CALLS}/", $jsCallBuffer, $headSection);
    }

    private function _renderComponents() {
        for ($i = 0; $i < count($this->_components); $i++) {
            $this->_output["HTML"] .= $this->_components[$i]->render();
        }
    }

    private function _end() {
        ob_start();
        require dirname(__FILE__) . '/RenderBusinessLogic/_endForm.phtml';
        $this->_output["HTML"] .= ob_get_clean();
    }

    private function _groupComponentsBySaveMode() {
        for ($i = 0; $i < count($this->_components); ++$i) {
            switch ($this->_components[$i]->getSaveMode()) {
                case AbstractComponent::ONE_TO_ONE:
                    array_push($this->_oneToOneInserts, $this->_components[$i]);
                    break;
                case AbstractComponent::ONE_TO_ONE:
                    array_push($this->_oneToManyInserts, $this->_components[$i]);
                    break;
                default:
                    break;
            }
        }
    }

    private function _renderOneToOneSaveLogicBusiness() {
        //$oneToOneSaveLogicBuffer = file_get_contents(dirname(__FILE__) ."/SaveBusinessLogic/_oneToOneSaveBusinessLogic.php");
        $oneToOneSaveLogicBuffer = <<<'PHPCODE'
   $insertOneToOne->addNameValueFieldPair("{DB_FIELD_NAME}", $_POST["send"]["{FORM_NAME}"]);
PHPCODE;
        for ($j = 0; $j < count($this->_oneToOneInserts); ++$j) {
            $search = array("/{DB_FIELD_NAME}/", "/{FORM_NAME}/");
            $replace = array($this->_oneToOneInserts[$j]->getDbFieldName(), $this->_oneToOneInserts[$j]->getFormName());
            $this->_oneToOneSaveLogicOutput .= preg_replace($search, $replace, $oneToOneSaveLogicBuffer);
        }
    }

    private function _renderSaveLogicBusiness() {
        $saveLogicBusinessOutput = file_get_contents(dirname(__FILE__) . "/SaveBusinessLogic/_generalSaveBusinessLogic.php");
        $saveLogicBusinessOutput = preg_replace("/{TABLE_NAME}/", $this->_tableName, $saveLogicBusinessOutput);
        $this->_groupComponentsBySaveMode();

        $this->_renderOneToOneSaveLogicBusiness();
        $saveLogicBusinessOutput = preg_replace("#//{INSERT_ONE_TO_ONE_FIELDS_VALUES}//#", $this->_oneToOneSaveLogicOutput, $saveLogicBusinessOutput);
        $this->_output["PHP"] .= $saveLogicBusinessOutput;
    }

    public function addComponent(AbstractComponent $component) {
        array_push($this->_components, $component);
    }

    public function setTableName($tableName) {
        $this->_tableName = $tableName;
    }

    public function render() {
        $this->_begin();
        $this->_renderComponents();
        $this->_end();
        $this->_renderSaveLogicBusiness();
        $outputFile = $this->_outputlFilePath . $this->_outputlFileName;
        file_put_contents($outputFile, ($this->_output["PHP"] . $this->_output["HTML"]));
    }

}